import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/components/loading_widget.dart';
import 'package:kxy_flutter_app/model/exam.dart';
import 'package:kxy_flutter_app/view/examination/template/time_line_item_widget.dart';
import 'package:provider/provider.dart';
import 'view_model/exam_rank_model.dart';

class ExamPickerWidget extends StatelessWidget {
  final int Function() getCateIndex;
  ExamPickerWidget({
    Key key,
    @required this.getCateIndex,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      textColor: Colors.white,
      child: Text('选择考试'),
      onPressed: () async {
        await showDialog(
          context: context,
          builder: (cxt) => examListWidget(context),
        );
      },
    );
  }

  Widget examListWidget(BuildContext context) {
    var examRankModel = Provider.of<ExamRankModel>(context, listen: false);
    var index = getCateIndex();
    var loader = examRankModel.getExamList(index);
    Function(String) checkCallback = (examId) => examRankModel.setExamId(index, examId);

    return Scaffold(
      appBar: AppBar(title: Text('请选择一场考试')),
      body: FutureBuilder<List<ExamProfile>>(
        future: loader,
        initialData: [],
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return buildExamList(snapshot.data, checkCallback, context);
          }
          return LoadingWidget();
        },
      ),
    );
  }

  Widget buildExamList(List<ExamProfile> examList, Function(String) checkCallback, BuildContext context) {
    var format = new DateFormat('yyyy年MM月');
    Map<String, List<ExamProfile>> map = new Map<String, List<ExamProfile>>();

    examList.forEach((item) {
      var key = format.format(item.date);
      if (!map.containsKey(key)) {
        map[key] = [];
      }
      map[key].add(item);
    });

    List<Widget> children = new List<Widget>();
    for (var item in map.keys) {
      children.add(dateText(item));

      for (var exam in map[item]) {
        children.add(examText(
          exam,
          examList.indexOf(exam) == examList.length - 1,
          checkCallback,
          context,
        ));
      }
    }

    return ListView(
      padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
      children: children,
    );
  }

  Widget dateText(String date) {
    return TimeLineItem(
      isPrimary: true,
      child: Text(
        date,
        style: TextStyle(
          fontSize: 18,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget examText(ExamProfile exam, bool isLatest, Function(String) checkCallback, BuildContext context) {
    var format = new DateFormat('yyyy-MM-dd HH:mm');
    return TimeLineItem(
      isLatest: isLatest,
      child: FlatButton(
        onPressed: () {
          checkCallback(exam.id);
          Navigator.pop(context, exam);
        },
        padding: EdgeInsets.all(0),
        color: Colors.white,
        child: ListTile(
          title: Text(exam.name),
          subtitle: Text(
            format.format(exam.date),
            style: TextStyle(
              fontSize: 12,
              color: Colors.black54,
            ),
          ),
        ),
      ),
    );
  }
}
